package gappengko;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** JSONP interface for putting a new map on the server.
 * Returns the key on the created Entity.
 *
 */
public class CreateServlet extends HttpServlet  {

  private static final long serialVersionUID = 1L;
  
  private HttpRequestHandler _handler = new HttpRequestHandler();

	/** creates an object on the server*/
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
    try {
      
      CrossDomainUtils.enableCrossOriginResourceSharing(req, res);    
      
      if (RecaptchaUtils.recaptchaEnabled()) {
        if (!RecaptchaUtils.verify(req))  {
          res.setStatus(HttpServletResponse.SC_FORBIDDEN);
          return;
        }
      }
      
		  String json = "{\"key\":\""+_handler.createEntityFromRequest(req)+"\"}";
      ContentNegotiationUtils.processRequest(req, res, json);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
	}

	/** doPost is required for large amount of data 
	 *
	 */
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		doGet(req, res);
	}
	
	@Override
	public void doOptions(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
	  CrossDomainUtils.doOptions(req, res);
	}

}
